package com.site_assembly.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DeadLetterRabbitConfig {

    public static final String DL_FINISH_EXCHANGE = "exchange.assign.finish";
    public static final String DL_FINISH_QUEUE = "queue.assign.finish";
    public static final String DL_FINISH_RK = "assign.finish";

    public static final String DEAD_LETTER_EXCHANGE = "exchange.dl.assign.finish";
    public static final String DEAD_FINISH_QUEUE = "queue.dl.assign.finish";
    public static final String DEAD_LETTER_RK = "dl.assign.finish";

    @Bean
    public TopicExchange topicExchange1() {
        return new TopicExchange(DL_FINISH_EXCHANGE);
    }

    @Bean
    public Queue queue1() {
        //指定正常队列的死信交换机
        Map<String, Object> arguments = new HashMap<>();

        arguments.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", DEAD_LETTER_RK);

        Queue queue = new Queue(DL_FINISH_QUEUE, false, false, false, arguments);
        return queue;
    }

    @Bean
    public Binding binding1() {
        return BindingBuilder.bind(queue1()).to(topicExchange1()).with(DL_FINISH_RK);
    }


    @Bean
    public TopicExchange deadLetterExchange() {
        return new TopicExchange(DEAD_LETTER_EXCHANGE);
    }

    @Bean
    public Queue dealLetterQueue() {
        return new Queue(DEAD_FINISH_QUEUE);
    }

    @Bean
    public Binding dealLetterBinding() {
        return BindingBuilder.bind(dealLetterQueue()).to(deadLetterExchange()).with(DEAD_LETTER_RK);
    }
}
